using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Ent.Web;
using Ent.Web.Helper;
using Ent.Web.WebControl;
using Ent.Web.WebControl.Parameters;
using Ent.Voice.Show;
public partial class Controls_Event_SetNomination : Ent.Web.WebControl.UserControl
{
    private Settings _pop;
    int minNomCount = 1;
    int minNomAmt = 0;
    int maxNomAmt = 99999;
    protected void Page_Load(object sender, EventArgs e)
    {
        string evStartDate = (Values[EventProperty.StartDate] != null ? Values[EventProperty.StartDate].ToString() : DateTime.Today.ToShortDateString());
        string evEndDate = (Values[EventProperty.DueDate] != null 
            ? Values[EventProperty.DueDate].ToString()
            : DateTime.Parse(evStartDate).AddMonths(Ent.Web.Helper.MathFunction.ConvertToInteger(GetASPXParam("maxDuration"), 2)).ToShortDateString());
        Dictionary<string, string> rep = new Dictionary<string, string>();

        rvParticipationStDate.MaximumValue = evEndDate;
        rvParticipationStDate.MinimumValue = evStartDate;
        rep = new Dictionary<string, string>();
        rep.Add("$1", rvParticipationStDate.MinimumValue);
        rep.Add("$2", rvParticipationStDate.MaximumValue);
        rep.Add("$3", "Nomination Start");
        rvParticipationStDate.ErrorMessage = ResourceHelper.Get("MSG_RVDueDate", rep);

        
        rep = new Dictionary<string, string>();
        rep.Add("$1", "Nomination Start Date");
        rfvParticipationStDate.ErrorMessage = ResourceHelper.Get("MSG_RFVRequired", rep);

        object maxNomCount = GetASPXParam("maxNomCount");
        rvMaxPartLimit.MaximumValue = maxNomCount != null ? maxNomCount.ToString() : "";
        rvMaxPartLimit.MinimumValue = minNomCount.ToString();
        rep = new Dictionary<string, string>();
        rep.Add("$1", rvMaxPartLimit.MinimumValue);
        rep.Add("$2", rvMaxPartLimit.MaximumValue);
        rep.Add("$3", "Maximum Nomination Count");
        rvMaxPartLimit.ErrorMessage = ResourceHelper.Get("MSG_RVMaxCount", rep);

        rep = new Dictionary<string, string>();
        rep.Add("$1", "Limited");
        rep.Add("$2", "Maximum Nomination Count");
        rfvMaxPartLimit.ErrorMessage = ResourceHelper.Get("MSG_RFVMaxCount", rep);

        bool ecom = false;
        Boolean.TryParse(ConfigurationManager.AppSettings["allowEcommerce"], out ecom);
        if (ecom)
        {
            trNomAmount.Visible = false;

            rep = new Dictionary<string, string>();
            rep.Add("$1", "Nomination Amount");
            rfvNomAmount.ErrorMessage = ResourceHelper.Get("MSG_RFVRequired", rep);

            rvNomAmount.MaximumValue = maxNomAmt.ToString();
            rvNomAmount.MinimumValue = minNomAmt.ToString();
            rep = new Dictionary<string, string>();
            rep.Add("$1", rvNomAmount.MinimumValue);
            rep.Add("$2", rvNomAmount.MaximumValue);
            rep.Add("$3", "Nomination Amount");
            rvNomAmount.ErrorMessage = ResourceHelper.Get("MSG_RVMaxCount", rep);
        }
        else
        {
            trNomAmount.Visible = false;
        }

        if (evStartDate != "" && txtParticipationStDate.Text == "")
            txtParticipationStDate.Text = evStartDate;

        lblEventStart.Text = " Event Starts On:";
        lblEventEnd.Text = " Event Ends On:";
        valEventStart.Text = DateTime.Today.ToShortDateString();
        valEventEnd.Text = evEndDate;

        btnShowCal.Attributes.Add("onclick", "show_cal(400, 160, this, '" + txtParticipationStDate.ClientID + "');");

        PopuplateControls();
    }
    public override void AddJavascript()
    {
        try
        {
            LoadJavascript("Javascripts/Settings.js");
            LoadJavascript("Javascripts/calendar.js");
            divJavascript.InnerHtml += JavascriptText;
        }
        catch (Exception exc)
        { }
    }
    public void PopuplateControls()
    {
        _pop = new Settings(Server.MapPath("Data/Settings.xml"));
        ddlCurrency.Items.AddRange(_pop.GetItemCollection("Currency"));
        cblNomineeTracking.Items.AddRange(_pop.GetItemCollection("UserTracking"));
        cblFileType.Items.AddRange(_pop.GetItemCollection("FileType"));
        rblMaxPart.Items.AddRange(_pop.GetItemCollection("MaxParticipation"));
    }
    public override void SetEvents()
    {
        if (cbParticipation.Checked)
        {
            OnLoadEnable("tabNomination");
            OnLoadExecute(new string[] { 
                "_ValidatorEnable(document.getElementById('" + rvParticipationStDate.ClientID + "'),true)" ,
                "_ValidatorEnable(document.getElementById('" + rfvParticipationStDate.ClientID + "'),true)"
            });
        }
        else
        {
            OnLoadDisable("tabNomination");
            OnLoadExecute(new string[] { 
                "_ValidatorEnable(document.getElementById('" + rvParticipationStDate.ClientID + "'),false)" ,
                "_ValidatorEnable(document.getElementById('" + rfvParticipationStDate.ClientID + "'),false)"
            });
        }
        if (cbParticipation.Checked && cbUpload.Checked)
            OnLoadEnable(cblFileType.ClientID);
        else
            OnLoadDisable(cblFileType.ClientID);

        if (cbParticipation.Checked && rblMaxPart.SelectedIndex == 1)
        {
            OnLoadEnable(txtMaxPartLimit.ClientID);
            OnLoadExecute(new string[] { 
                "_ValidatorEnable(document.getElementById('" + rfvMaxPartLimit.ClientID + "'),true)" ,
                "_ValidatorEnable(document.getElementById('" + rvMaxPartLimit.ClientID + "'),true)"
            });
        }
        else
        {
            OnLoadDisable(txtMaxPartLimit.ClientID);
            OnLoadExecute(new string[] { 
                "_ValidatorEnable(document.getElementById('" + rfvMaxPartLimit.ClientID + "'),false)" ,
                "_ValidatorEnable(document.getElementById('" + rvMaxPartLimit.ClientID + "'),false)"
            });
        }
        OnclickParam onclickMaxPart = new OnclickParam(rblMaxPart, rblMaxPart.ClientID + "_1", txtMaxPartLimit.ClientID, 
            new string[] { "_ValidatorEnable(document.getElementById('" + rfvMaxPartLimit.ClientID + "'),!document.getElementById('" + txtMaxPartLimit.ClientID + "').disabled)", 
                "_ValidatorEnable(document.getElementById('" + rvMaxPartLimit.ClientID + "'),!document.getElementById('" + txtMaxPartLimit.ClientID + "').disabled)" },
            null);
        OnclickParam onclickUpload = new OnclickParam(cbUpload, null, cblFileType.ClientID, null, null);
        SetOnClickEvent(new OnclickParam(cbParticipation, null, "tabNomination",
            new string[] { "_ValidatorEnable(document.getElementById('" + rfvMaxPartLimit.ClientID + "'),!document.getElementById('" + txtMaxPartLimit.ClientID + "').disabled)", 
                "_ValidatorEnable(document.getElementById('" + rvMaxPartLimit.ClientID + "'),!document.getElementById('" + txtMaxPartLimit.ClientID + "').disabled)",
                "_ValidatorEnable(document.getElementById('" + rfvParticipationStDate.ClientID + "'),!document.getElementById('" + txtParticipationStDate.ClientID + "').disabled)",
                "_ValidatorEnable(document.getElementById('" + rvParticipationStDate.ClientID + "'),!document.getElementById('" + txtParticipationStDate.ClientID + "').disabled)"
                }, 
            new OnclickParam[] { onclickMaxPart, onclickUpload }));
        SetOnClickEvent(onclickMaxPart);
        SetOnClickEvent(onclickUpload);
    }
    public override void Init()
    {
        foreach (string key in Values.Keys)
        {
            if (Values[key] == null)
                continue;
            switch (key)
            {
                
                case EventProperty.NomineeTracking:
                    SetCollectionControl(cblNomineeTracking, (string[])Values[key]);
                    break;
                case EventProperty.Participation:
                    cbParticipation.Checked = Convert.ToBoolean(Values[key]);
                    break;
                case EventProperty.ParticipationStDate:
                    txtParticipationStDate.Text = Values[key].ToString();
                    break;
                case EventProperty.MaxPart:
                    SetCollectionControl(rblMaxPart, (string[])Values[key]);
                    break;
                case EventProperty.MaxPartLimit:
                    txtMaxPartLimit.Text = Values[key].ToString();
                    break;
                case EventProperty.NomAmount:
                    txtNomAmount.Text = Values[key].ToString();
                    break;
                case EventProperty.NCurrency:
                    ddlCurrency.SelectedIndex = ddlCurrency.Items.IndexOf(ddlCurrency.Items.FindByValue(Values[key].ToString()));
                    break;
                case EventProperty.Upload:
                    cbUpload.Checked = Convert.ToBoolean(Values[key]);
                    break;
                case EventProperty.FileType:
                    SetCollectionControl(cblFileType, (string[])Values[key]);
                    break;
                default:
                    break;
            }
        }
    }
    public override void Dispose()
    {
        //throw new Exception("The method or operation is not implemented.");
    }
    public override Dictionary<string, object> GetValues()
    {
        Dictionary<string, object> retValue = new Dictionary<string, object>();
        
        #region Nomination
        retValue.Add(EventProperty.Participation, cbParticipation.Checked);
        retValue.Add(EventProperty.ParticipationStDate, txtParticipationStDate.Text);
        retValue.Add(EventProperty.MaxPart, Ent.Web.WebControl.Helper.GetSelectedValues(rblMaxPart.Items));
        retValue.Add(EventProperty.MaxPartLimit, txtMaxPartLimit.Text);
        retValue.Add(EventProperty.NomAmount, txtNomAmount.Text);
        retValue.Add(EventProperty.NCurrency, ddlCurrency.SelectedValue);
        retValue.Add(EventProperty.Upload, cbUpload.Checked);
        retValue.Add(EventProperty.FileType, Ent.Web.WebControl.Helper.GetSelectedValues(cblFileType.Items));
        retValue.Add(EventProperty.NomineeTracking, Ent.Web.WebControl.Helper.GetSelectedValues(cblNomineeTracking.Items));
        #endregion
        
        return retValue;
    }
}
